#include <cstdio>
#include <algorithm>

using namespace std;

struct Hotel
{
    int num, room, cost;
};

bool operator < ( Hotel a, Hotel b )
{
    return a.cost < b.cost;
}

int n, pos, query[ 256 ];
Hotel h[ 101 ];

void init( )
{
    int i, j;
    char data[ 20 ];
    scanf("%d", &n);
    gets( data );
    for ( i = 0; i < n; i++ )
    {
        gets( data );
        j = 0;
        h[ i ].num = h[ i ].room = h[ i ].cost = 0;
        while ( data[ j ] != ' ' )
            h[ i ].num = h[ i ].num * 10 + data[ j++ ] - '0';
        j++;
        while ( data[ j ] != ' ' )
            h[ i ].room = h[ i ].room * 10 + data[ j++ ] - '0';
        j++;
        while ( data[ j ] != ' ' && j < strlen( data ) )
            h[ i ].cost = h[ i ].cost * 10 + data[ j++ ] - '0';
    }
    pos = 1;
    while ( pos < n )
        pos <<= 1;
    sort( h, h + n );
    for ( i = pos; i < pos << 1; i++ )
    {
        if ( i - pos < n )
            query[ i ] = h[ i - pos ].room;
        else
            query[ i ] = 0;
    }
    for ( i = pos - 1; i > 0; i-- )
    {
        query[ i ] = query[ ( i << 1 ) ];
        if ( query[ i ] < query[ ( i << 1 ) + 1 ] )
            query[ i ] = query[ ( i << 1 ) + 1 ];
    }
}

void work( )
{
    int i, t, p, bu;
    char data[ 3 ];
    scanf("%d", &t);
    gets( data );
    while ( t-- )
    {
        gets( data );
        p = data[ 0 ] - '0';
        if ( data[ 1 ] == '0' )
            p = 10;
        if ( query[ 1 ] < p )
            puts("sorry");
        else
        {
            i = 1;
            while ( i < pos )
            {
                i <<= 1;
                if ( query[ i ] < p )
                    i++;
            }
            printf("%d\n", h[ i - pos ].num);
            query[ i ] -= p;
            i >>= 1;
            while ( i )
            {
                bu = query[ i ];
                query[ i ] = query[ ( i << 1 ) ];
                if ( query[ i ] < query[ ( i << 1 ) + 1 ] )
                    query[ i ] = query[ ( i << 1 ) + 1 ];
                if ( query[ i ] == bu )
                    break;
                i >>= 1;
            }
        }
    }
}

int main( )
{
    int t;
    scanf("%d", &t);
    while ( t-- )
    {
        init( );
        work( );
    }
    return 0;
}
